#include "OBJLoader.h"


OBJLoader::OBJLoader(Scene *_s){
	theScene = _s;
	_curShade = NULL;
}


OBJLoader::~OBJLoader(){
}



//loads the OBJ object into the scene
bool OBJLoader::loadToScene(const char* _fin){
	ifstream input(_fin);
	if (!input.good()){
		return false;
	}

	string line;
	string command;
	while (!input.eof()){
		line.clear();
		getline(input, line);
		//add a space to the end of line
		line.append(" ");
		istringstream lineInput(line);
		if (lineInput.eof()){
			continue;
		}
		command.clear();
		lineInput >> command;
		if (lineInput.fail()){
			continue;
		}
		if (command[0] == '#'){
			//comment
		}
		else if (command == "v"){
			vec3 v;
			lineInput >> v.x;
			lineInput >> v.y;
			lineInput >> v.z;
			theVerts.push_back(v);
			//printf("push vertex\n");
		}
		else if (command == "vn"){
			//it's a vertex normal
		}
		else if (command == "vt"){
			//it's a texture coordinate
		}
		else if (command == "f"){
			//printf("found a face\n");
			//set of vertices for a given face
			vector<int> verts;
			//it's a face
			int vi;
			lineInput >> vi;
			

			
			while (!lineInput.fail() && lineInput.good()){
				int uvi, ni;
				bool gotUV = false;
				bool gotN = false;
				if (lineInput.peek() == '/'){
					char slash;
					lineInput >> slash;
					if (lineInput.peek() == '/'){
						lineInput >> slash;
						lineInput >> ni;
						gotN = true;
					}
					else{
						lineInput >> uvi;
						gotUV = true;
						if (lineInput.peek() == '/'){
							lineInput >> slash;
							lineInput >> ni;
							gotN = true;
						}
					}
				}
				vi = vi > 0 ? vi - 1 : (int)theVerts.size() + vi;
				//printf("push vertex for face\n");
				verts.push_back(vi);
				if (vi >= (int)theVerts.size()){
					printf("Found out-of-range vertex index: %i\n", vi);
					return false;
				}
				if (gotUV){
					//you got a UV
				}
				if (gotN){
					//you got an N
				}
				lineInput >> vi;
			}
			
			if (verts.size() >= 3){
				//printf("push triangle\n");
				//add the triangle
				Triangle *theTri = new Triangle(vec4(0, 0, 0, 0), vec3(), theVerts.at(verts[0]), theVerts.at(verts[1]), theVerts.at(verts[2]));
				theTri->shader(_curShade);
				//theScene->addTriangle(theVerts.at(verts[0]), theVerts.at(verts[1]), theVerts.at(verts[2]));
				theScene->addShape(theTri);
			}
			
		}
		else if (command == "usemtl"){
			//you got a material
		}
		else if (command == "mtllib"){
			//you got a material lib
		}
		else if (command == "s"){
			//something with an s
		}
		else{
			//I don't even know what else
		}
	}

	for (int z = 0; z < (int)theVerts.size(); z++){
		//printVec3(theVerts.at(z));
	}

	return true;
/*
	string line;
	//open the file
	ifstream theFile(_fin);
	if (theFile.is_open()){
		//for each line
		int i = 0;
		while (getline(theFile, line)){
			//printf(line.c_str());
			i++;
			//if it's a comment, skip it
			//if it starts with "v "
			if (line.substr(0, 2).compare("v ") == 0){
				printf("We've got a vertex\n");
				//add the vertex to a vector of vertices
				vector<float> nums;
				int s = 2;
				int sEnd = 2;
				while (sEnd < (int)line.size() || line.at(sEnd) != '\n'){
					while (!isspace(line.at(sEnd))){
						sEnd++;
					}
					float _v = atof(line.substr(s, sEnd - s).c_str());
					nums.push_back(_v);
					s = sEnd + 1;
					sEnd = s;
				}
				if (nums.size() == 3){
					theVerts.push_back(vec3(nums[0], nums[1], nums[2]));
					printVec3(theVerts.back());
				}
			}
			else if (line.substr(0, 2).compare("f ") == 0){
				printf("We've got a face\n");
				//if it starts with "f "
					//if it has 3 vertices
					//add the triangle
				//else if it has 4 vertices
					//split it into two triangles
					//add the triangles
			}
		}
	}
	else{
		return false;
	}
	
	return true;
	*/
}

void OBJLoader::printVec3(vec3 _in){
	printf("(%f, %f, %f)\n", _in.x, _in.y, _in.z);
}
